﻿using System.Diagnostics;
using System.Text.RegularExpressions;

namespace WSPay.Common
{
    public static class RegularExtensions
    {
        public static Regex Email = new Regex(@"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", RegexOptions.Singleline | RegexOptions.Compiled);

        public static Regex Mobile = new Regex(@"^1[3-9][0-9]\d{8}$",RegexOptions.Singleline|RegexOptions.Compiled);

        public static Regex IDCard = new Regex(@"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$",RegexOptions.Singleline|RegexOptions.Compiled);

        public static Regex WebUrl = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?|(http|https)://localhost:\d{2,}[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.Singleline | RegexOptions.Compiled);

        public static Regex Password = new Regex(@"^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$", RegexOptions.Singleline | RegexOptions.Compiled);

        public static Regex UserName = new Regex(@"^[A-Za-z_]\w{3,16}$", RegexOptions.Singleline | RegexOptions.Compiled);

        [DebuggerStepThrough]
        public static bool Match(this string target, Regex regex)
        {
            return regex.IsMatch(target);
        }

        [DebuggerStepThrough]
        public static bool MatchAndNotNull(this string target, Regex regex)
        {
            return !string.IsNullOrEmpty(target) && regex.IsMatch(target);
        }

        [DebuggerStepThrough]
        public static bool IsWebUrl(this string target)
        {
            return target.MatchAndNotNull(WebUrl);
        }

        [DebuggerStepThrough]
        public static bool IsEmail(this string target)
        {
            return target.MatchAndNotNull(Email);
        }
    }
}
